using System;
using System.Collections.Generic;
using System.Linq;

using Csla;

using EntModApp.EntServices.Business;
using EntModApp.EntServices.IoC;

using DemoApp.DataAccess;

namespace DemoApp.BusinessLayer 
{
  [Serializable]
  public partial class CslaUserEdit : 
    BusinessBase<CslaUserEdit>,
    IUser
  {
    #region [ Business Properties ]

    public static PropertyInfo<Guid> UserIdPropertyInfo = RegisterProperty(new PropertyInfo<Guid>("UserId"));
    public Guid UserId 
    { 
      get
      {
        return GetProperty(UserIdPropertyInfo);
      }
      private set
      {
        LoadProperty(UserIdPropertyInfo, value);
      }
    }

    public static PropertyInfo<string> UserNamePropertyInfo = RegisterProperty(new PropertyInfo<string>("UserName"));
    public string UserName 
    { 
      get
      {
        return GetProperty(UserNamePropertyInfo);
      }
      set
      {
        LoadProperty(UserNamePropertyInfo, value);
      }
    }

    public static PropertyInfo<string> PasswordPropertyInfo = RegisterProperty(new PropertyInfo<string>("Password"));
    public string Password 
    { 
      get
      {
        return GetProperty(PasswordPropertyInfo);
      }
      set
      {
        LoadProperty(PasswordPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaRoleEditCollection> RolesPropertyInfo = RegisterProperty(new PropertyInfo<CslaRoleEditCollection>("Roles"));
    public CslaRoleEditCollection Roles 
    { 
      get
      {
        return GetProperty(RolesPropertyInfo);
      }
      private set
      {
        LoadProperty(RolesPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaGroupEditCollection> GroupsPropertyInfo = RegisterProperty(new PropertyInfo<CslaGroupEditCollection>("Groups"));
    public CslaGroupEditCollection Groups 
    { 
      get
      {
        return GetProperty(GroupsPropertyInfo);
      }
      private set
      {
        LoadProperty(GroupsPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaSubjectEditCollection> CreatedSubjectsPropertyInfo = RegisterProperty(new PropertyInfo<CslaSubjectEditCollection>("CreatedSubjects"));
    public CslaSubjectEditCollection CreatedSubjects 
    { 
      get
      {
        return GetProperty(CreatedSubjectsPropertyInfo);
      }
      private set
      {
        LoadProperty(CreatedSubjectsPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaSubjectEditCollection> EditedSubjectsPropertyInfo = RegisterProperty(new PropertyInfo<CslaSubjectEditCollection>("EditedSubjects"));
    public CslaSubjectEditCollection EditedSubjects 
    { 
      get
      {
        return GetProperty(EditedSubjectsPropertyInfo);
      }
      private set
      {
        LoadProperty(EditedSubjectsPropertyInfo, value);
      }
    }

    #endregion [ Business Properties ]

    #region [ Validation Rules ]
    #endregion [ Validation Rules ]

    #region [ Authorization Rules ]
    #endregion [ Authorization Rules ]
	
    #region [ Factory Methods ]

    #region [ Root Factory Methods ]
    public static CslaUserEdit New()
    {
      return DataPortal.Create<CslaUserEdit>();
    }

    public static void Delete(UserKey key)
    {
      DataPortal.Delete<User>(new SingleCriteria<User, UserKey>(key));
    }

    public static void Delete(Guid UserId)
    {
      Delete(new UserKey { UserId = UserId });
    }
    #endregion [ Root Factory Methods ]

    #region [ Child Factory Methods ]
    internal static CslaUserEdit NewChild()
    {
      return DataPortal.CreateChild<CslaUserEdit>();
    }

    internal static CslaUserEdit GetUserEdit(User data, UserLoadSettings loadSettings)
    {
      if (data == null) throw new ArgumentNullException("data");
      if (loadSettings == null) throw new ArgumentNullException("loadSettings");
      CslaUserEdit result;
      if (data is Person)
        result = CslaPersonEdit.GetPersonEdit((Person)data, PersonLoadSettings.FromUserLoadSettings(loadSettings));
      else
      if (data is Group)
        result = CslaGroupEdit.GetGroupEdit((Group)data, GroupLoadSettings.FromUserLoadSettings(loadSettings));
      else
      {
        result = new CslaUserEdit();
        result.FetchUser(data, loadSettings);
      }
      result.MarkAsChild();
      return result;
    }
    #endregion [ Child Factory Methods ]

    private static int ForceInitField = 0;
    protected CslaUserEdit() 
    {
      ForceInitField = 1;
    }
    #endregion [ Factory Methods ]

    #region [ Data Access ]

    #region [ Root Data Access ]
    protected override void DataPortal_Create()
    {
      base.DataPortal_Create();
    }

    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_Insert()
    {
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_Update()
    {
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_DeleteSelf()
    {
      DataPortal_Delete(new SingleCriteria<User, UserKey>(new UserKey { UserId = this.UserId }));
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    private void DataPortal_Delete(SingleCriteria<User, UserKey> criteria)
    {
    }
    #endregion [ Root Data Access ]

    #region [ Child Data Access ]
    protected override void Child_Create()
    {
      base.Child_Create();
    }
    
    private void Child_Fetch(object childData)
    {
    }
    
    private void Child_Insert(object parent)
    {
    }
    
    private void Child_Update(object parent)
    {
    }
    
    private void Child_DeleteSelf(object parent)
    {
    }
    #endregion [ Child Data Access ]

    protected void FetchUser(User data, UserLoadSettings loadSettings)
    {
      FetchUserData(data, loadSettings);
      FetchUserChildren(data, loadSettings);
    }

    protected void FetchUserData(User data, UserLoadSettings loadSettings)
    {
      this.UserId = data.UserId;
      this.UserName = data.UserName;
      this.Password = data.Password;
    }

    protected void FetchUserChildren(User data, UserLoadSettings loadSettings)
    {
      if (loadSettings.Inclusion.IncludeRoles)
        this.Roles = CslaRoleEditCollection.GetRoleEditCollection(data.Roles, loadSettings.Inclusion.IncludedRolesSettings);
      if (loadSettings.Inclusion.IncludeGroups)
        this.Groups = CslaGroupEditCollection.GetGroupEditCollection(data.Groups, loadSettings.Inclusion.IncludedGroupsSettings);
      if (loadSettings.Inclusion.IncludeCreatedSubjects)
        this.CreatedSubjects = CslaSubjectEditCollection.GetSubjectEditCollection(data.CreatedSubjects, loadSettings.Inclusion.IncludedCreatedSubjectsSettings);
      if (loadSettings.Inclusion.IncludeEditedSubjects)
        this.EditedSubjects = CslaSubjectEditCollection.GetSubjectEditCollection(data.EditedSubjects, loadSettings.Inclusion.IncludedEditedSubjectsSettings);
    }
    #endregion [ Data Access ]

    #region [ Explicit IUser implementation ]

    Guid IUser.UserId
    {
      get { return this.UserId; }
    }

    string IUser.UserName
    {
      get { return this.UserName; }
      set
      {
        this.UserName = value;
      }
    }

    string IUser.Password
    {
      get { return this.Password; }
      set
      {
        this.Password = value;
      }
    }

    IBusinessObjectCollection<IRole> IUser.Roles
    { 
      get
      {
        return this.Roles;
      }
    }

    IBusinessObjectCollection<IGroup> IUser.Groups
    { 
      get
      {
        return this.Groups;
      }
    }

    IBusinessObjectCollection<ISubject> IUser.CreatedSubjects
    { 
      get
      {
        return this.CreatedSubjects;
      }
    }

    IBusinessObjectCollection<ISubject> IUser.EditedSubjects
    { 
      get
      {
        return this.EditedSubjects;
      }
    }


    bool IBusinessObject.IsReadonly
    {
      get { return false; }
    }

    bool IBusinessObject.IsDirty
    {
      get { return this.IsDirty; }
    }

    #endregion [ Explicit IUser implementation ]
  }
}
